package com.changgou.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.SeckillStatus;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/** 秒杀的监听 */
@Component
@RabbitListener(queues = "orderListenerSecillQuery")
public class DelaressageListener {
@Autowired
private SeckillOrderService seckillOrderService;
  @Autowired private RedisTemplate redisTemplate;
  @RabbitHandler
  public void getMessage(String message) {
    SeckillStatus seckillStatus = JSON.parseObject(message, SeckillStatus.class);
    // 如果此时redis中没有用户排对的信息，则表明该订单已经处理，如果有用户排队的信息，则表明该订单尚未支付，回滚库存，取消支取
    Object userQueueStatus = redisTemplate.boundHashOps("UserQueueStatus").get(seckillStatus.getUsername());
    if (userQueueStatus!=null){
      // 关闭微信支付
      // 删除订单
      seckillOrderService.deleteOrder(seckillStatus.getUsername());
    }
  }
}
